package com.tao.config;

import com.alibaba.fastjson.JSON;
import com.tao.pojo.SysLog;
import com.tao.pojo.User;
import com.tao.pojo.vo.LoginUser;
import com.tao.service.SysLogService;
import com.tao.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    SysLog sysLog = new SysLog();

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( com.tao.config.MyLog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        //保存日志

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作
        MyLog myLog = method.getAnnotation(MyLog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperation(value);//保存获取的操作
        }

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        sysLog.setMethod(className + "." + methodName);

        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        String params = JSON.toJSONString(args);
        List<User> userList = JSON.parseArray(params, User.class);
        if (!userList.isEmpty()) {
            User user = userList.get(0);
            sysLog.setUsername(user.getUsername());
            // 访问用户对象的其他属性
        }
        sysLog.setParams(params);
        sysLog.setCreateDate(new Date());
        //获取用户名
        String token = request.getHeader("token");
        if (!StringUtils.hasText(token)) {
            //放行
            return;
        }
        //解析token
        String userid;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userid = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        //从redis中获取用户信息
        LoginUser loginUser = (LoginUser) redisTemplate.opsForValue().get("login:" + userid);
        sysLog.setUsername(loginUser.getUser().getUsername());
        //获取用户ip地址
        sysLog.setIp(request.getRemoteAddr());
        //调用service保存SysLog实体类到数据库
        sysLogService.save(sysLog);
    }
}
